package com.gouda.merchant.controller.rbac;

import com.themis.constant.FrontConstant;
import com.themis.dto.BackendSystemUserDto;
import com.themis.dto.FrontRoleDto;
import com.themis.dto.SystemRoleDto;
import com.themis.enums.EnumIsEnable;
import com.themis.service.SystemRoleService;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.gouda.core.service.systemManage.MerchantUserService;
import com.gouda.merchant.controller.AbstractController;
import com.gouda.merchant.exception.ErrorCode;
import org.amethyst.atlas.basic.component.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by KGL on 2015/8/14.
 */
@Controller
@RequestMapping("/merchantRole")
public class MerchantSystemRoleController extends AbstractController {

    @Autowired
    private SystemRoleService roleService;
    @Autowired
    private MerchantUserService userService;

    @RequestMapping("/getMerchantRoles")
    public @ResponseBody
    Map<String, Object> getSystemRoles(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String loginUserId = this.getAdminUserId(request);

        String roleCode = request.getParameter("roleCode");
        String roleName = request.getParameter("roleName");
        String roleStatus = request.getParameter("roleStatus");
        String pageNum = request.getParameter("pageNum");

        if (Strings.isNullOrEmpty(loginUserId)){
            resultMap.put("code", ErrorCode.FAILED.getCode());
            resultMap.put("msg", ErrorCode.FAILED.getMessage());
            return resultMap;
        }

        BackendSystemUserDto loginUserDto = userService.getUserById(loginUserId);

        PageData<SystemRoleDto> pageDto = roleService.loadRolesByPrivilege(loginUserDto, roleCode, roleName, roleStatus, pageNum, FrontConstant.DEFAULT_PAGE_SIZE);
        resultMap.put("code", operateResult.getCode());
        resultMap.put("pageModel", pageDto);
        return resultMap;
    }

    @RequestMapping("/getMerchantAvailableRoles")
    public @ResponseBody
    Map<String, Object> getSystemAvailableRoles(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String loginUserId = this.getAdminUserId(request);

        String pageNum = request.getParameter("pageNum");
        if (Strings.isNullOrEmpty(loginUserId)){
            resultMap.put("code", ErrorCode.FAILED.getCode());
            resultMap.put("msg", ErrorCode.FAILED.getMessage());
            return resultMap;
        }

        BackendSystemUserDto loginUserDto = userService.getUserById(loginUserId);

        final String queryStatus = EnumIsEnable.YES.getCode() + "";
        PageData<SystemRoleDto> pageDto = roleService.loadRolesByPrivilege(loginUserDto, null, null, queryStatus, pageNum, FrontConstant.MINER_PAGE_SIZE);
        resultMap.put("code", operateResult.getCode());
        resultMap.put("pageModel", pageDto);
        return resultMap;
    }

    @RequestMapping("/modifyRole")
    public @ResponseBody
    Map<String, Object> modifyRole(@RequestBody FrontRoleDto roleDto, HttpServletRequest request){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String loginUserId = this.getAdminUserId(request);

        final String roleId = roleDto.getRoleId();
        final String roleName = roleDto.getModifyRoleName();
        final String roleStatus = roleDto.getModifyRoleStatus();
        if (Strings.isNullOrEmpty(roleId)){
            resultMap.put("code", ErrorCode.FAILED.getCode());
            resultMap.put("msg", ErrorCode.FAILED.getMessage());
            return resultMap;
        }
        if(!Strings.isNullOrEmpty(roleName) || !Strings.isNullOrEmpty(roleStatus)){
            roleService.modifyRole(loginUserId, roleId, roleName, roleStatus);
        }
        resultMap.put("code", operateResult.getCode());
        return resultMap;
    }

    @RequestMapping("/addRole")
    public @ResponseBody
    Map<String, Object> addRole(@RequestBody FrontRoleDto roleDto, HttpServletRequest request){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String loginUserId = this.getAdminUserId(request);

        final String roleCode = roleDto.getRoleCodeAdd();
        final String roleName = roleDto.getRoleNameAdd();
        if (Strings.isNullOrEmpty(roleCode) || Strings.isNullOrEmpty(roleName)){
            resultMap.put("code", ErrorCode.FAILED.getCode());
            resultMap.put("msg", ErrorCode.FAILED.getMessage());
            return resultMap;
        }

        //校验编码唯一性
        if(roleService.checkExist(roleCode)){
            resultMap.put("code", ErrorCode.EC_400005.getCode());
            resultMap.put("msg", ErrorCode.EC_400005.getMessage());
            return resultMap;
        }

        BackendSystemUserDto loginUserDto = userService.getUserById(loginUserId);

        roleService.addRole(loginUserDto, roleCode, roleName);
        resultMap.put("code", operateResult.getCode());
        return resultMap;
    }

    @RequestMapping("/authRoleFunction")
    public @ResponseBody
    Map<String, Object> authRoleFunction(@RequestBody FrontRoleDto roleDto){
        Map<String, Object> resultMap = Maps.newHashMap();
        ErrorCode operateResult = ErrorCode.SUCCESS;

        final String roleId = roleDto.getAuthRoleId();
        final String functionsStr = roleDto.getAuthRoleFunctionStr();
        if (Strings.isNullOrEmpty(roleId) || Strings.isNullOrEmpty(functionsStr)){
            resultMap.put("code", ErrorCode.FAILED.getCode());
            resultMap.put("msg", ErrorCode.FAILED.getMessage());
            return resultMap;
        }

        roleService.authRole(roleId, functionsStr);
        resultMap.put("code", operateResult.getCode());
        return resultMap;
    }

}
